package rtool

import (
	"fmt"
	"sync"
	"time"
)

type AccUniqueIdQueue struct {
	lock      sync.Mutex
	IndexID   int64
	QueueList []string
}

func NewUniqueIdQueue() *AccUniqueIdQueue {
	return &AccUniqueIdQueue{
		lock:      sync.Mutex{},
		IndexID:   time.Now().UnixNano(),
		QueueList: make([]string, 0),
	}
}

func (q *AccUniqueIdQueue) RPush(uniqueID string) {
	q.lock.Lock()
	defer q.lock.Unlock()
	q.QueueList = append(q.QueueList, uniqueID)
}

func (q *AccUniqueIdQueue) LPush(uniqueID string) {
	q.lock.Lock()
	defer q.lock.Unlock()
	q.QueueList = append([]string{uniqueID}, q.QueueList...)
}

func (q *AccUniqueIdQueue) AutoUniqueID() string {
	q.lock.Lock()
	defer q.lock.Unlock()
	if len(q.QueueList) <= 0 {
		q.IndexID++
		rt := fmt.Sprint(q.IndexID)
		return rt
	}
	rt := q.QueueList[0]
	q.QueueList = q.QueueList[1:]
	return rt
}
